/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.scene;

import com.flexengine.component.HorizontalTextAlign;
import com.flexengine.component.Label;
import com.flexengine.engine.FlexEngine;
import com.flexengine.input.InputManager;
import com.flexengine.utils.FontManager;
import com.flexengine.utils.ResourceManager;
import com.flexengine.utils.TextureManager;
import com.flexengine.view.GameWindow;
import com.flexengine.view.Graphics;
import com.flexengine.view.Texture;
import java.awt.AlphaComposite;
import java.awt.Font;
import java.io.InputStream;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public final class EngineIntroScene extends GameScene {
    
    private boolean done;

    private int     show;
    
    private Texture texture;
    
    private int     textureX;
    private int     textureY;
    
    private float   alpha;
    private float   updateAlpha;
    
    private final String credits;
    private final Font   creditsFont;
    private final Label  labelCredits;
    private final Font   gameNameFont;
    private final Label  gameName;
    
    private int stayVisible;
    
    public EngineIntroScene() {
        super("FlexEngineState");
        done = false;
        // --
        InputStream in = ResourceManager.getResourceAsStream("ElementalWare.png");
        //Logger.logMessage("FlexEngineState - in = "+in);
        texture = TextureManager.getInstance().get("ElementalWare",in);
        TextureManager.getInstance().get("FlexEngine",ResourceManager.getResourceAsStream("FlexEngine.png"));
        // --
        textureX = (int) (GameWindow.getInstance().getCenterX() - texture.getWidth()/2);
        textureY = (int) (GameWindow.getInstance().getCenterY() - texture.getHeight()/2);
        // --
        alpha = 0f;
        updateAlpha = 0.01f;
        // -- 
        show  = 0;
        // ---------------------------------------------------------------------
        credits = "Created by Luann R. Athayde 2013\u00A9 | All rights reserved.";
        creditsFont = FontManager.getFont("Arial",1,12);
        
        labelCredits = new Label(credits);
        labelCredits.setFont(creditsFont);
        labelCredits.setSize(640, 30);
        labelCredits.setHorizontalTextAlign(HorizontalTextAlign.HORIZONTAL_ALIGN_CENTER);
        labelCredits.setPosition(
                Label.SCREEN_HORIZONTAL_CENTER_ALIGN,
                Label.SCREEN_VERTICAL_BOTTOM_ALIGN);
        //this.add(labelCredits);
        
        gameNameFont = FontManager.getFont("FiveFingerDiscount", 1, GameWindow.getInstance().getHeight()/10);
        gameName = new Label();
        gameName.setFont(gameNameFont);
        gameName.setSize(800, 200);
        gameName.setHorizontalTextAlign(HorizontalTextAlign.HORIZONTAL_ALIGN_CENTER);
        gameName.setPosition(
                Label.SCREEN_HORIZONTAL_CENTER_ALIGN,
                Label.SCREEN_VERTICAL_CENTER_ALIGN);
        gameName.setVisible(false);
        this.add(gameName);
    }

    @Override
    public synchronized void update(Long time, Long totalTime) {
        textureX = (int) (GameWindow.getInstance().getCenterX() - texture.getWidth()/2);
        textureY = (int) (GameWindow.getInstance().getCenterY() - texture.getHeight()/2);
        labelCredits.setPosition(
                Label.SCREEN_HORIZONTAL_CENTER_ALIGN,
                Label.SCREEN_VERTICAL_BOTTOM_ALIGN);
        gameName.setPosition(
                Label.SCREEN_HORIZONTAL_CENTER_ALIGN,
                Label.SCREEN_VERTICAL_CENTER_ALIGN);
        gameName.setText(FlexEngine.getInstance().getGameName());
        super.update(time,totalTime);
        alpha += updateAlpha;
        if( alpha < 0f || alpha > 1f ) {
            alpha = alpha>1f? 1f : 0f;
            if( alpha==1f && 
                    stayVisible > FlexEngine.getInstance().getDesireUPS()*3 ) { // Wait approximaly 5 seconds...
                updateAlpha = -updateAlpha;
                show++;
                stayVisible = 0;
            } else if(alpha==0f) {
                updateAlpha = -updateAlpha;
                show++;
                stayVisible = 0;
            }
            stayVisible++;
        }
        if( show==2 ) {
            texture = TextureManager.getInstance().get("FlexEngine");
        }
        if( show == 4 ) {
            gameName.setVisible(true);
        }
        if( show >= 6 ) {
            done = true;
            SceneManager.getInstance().changeScene();
        }
        if( InputManager.isKeyDown(InputManager.VK_ENTER) ) {
            done = true;
            SceneManager.getInstance().changeScene();
        }
    }
    
    @Override
    public synchronized void render(Graphics g) {
        Graphics gTex = g.create();
        gTex.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        super.render(gTex);
        if( show < 4) {
            gTex.draw(texture, textureX, textureY);
            gTex.dispose();
        }
        labelCredits.render(g);
    }

    @Override
    public void end(SceneBundle bundle) {
        super.end(bundle);
        TextureManager.getInstance().remove("ElementalWare");
        TextureManager.getInstance().remove("FlexEngine");
    }
    
    public boolean isDone() {
        return done;
    }

    @Override
    public boolean isVisible() {
        return true;
    }
}
